package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsItems;
import com.ruoyi.wms.service.IWmsItemsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料管理Controller
 * 
 * @author ruoyi
 * @date 2021-10-13
 */
@RestController
@RequestMapping("/wms/wmsItems")
public class WmsItemsController extends BaseController
{
    @Autowired
    private IWmsItemsService wmsItemsService;

    /**
     * 获取物料列表
     */
    @PreAuthorize(hasPermi="wms:items:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsItems wmsItems)
    {
        startPage();
        List<WmsItems> list = wmsItemsService.selectItemsList(wmsItems);
        return getDataTable(list);
    }


    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize(hasPermi="wms:items:query")
    @GetMapping(value = { "/", "/{itemUuid}" })
    public AjaxResult getInfo(@PathVariable(value = "itemUuid", required = false) String itemUuid)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(itemUuid))
        {
            ajax.put(AjaxResult.DATA_TAG, wmsItemsService.selectItemsByUuid(itemUuid));
        }
        return ajax;
    }

    /**
     * 导出物料管理列表
     */
    @PreAuthorize(hasPermi = "wms:wmsItems:export")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsItems wmsItems) throws IOException
    {
        List<WmsItems> list = wmsItemsService.selectItemsList(wmsItems);
        ExcelUtil<WmsItems> util = new ExcelUtil<WmsItems>(WmsItems.class);
        util.exportExcel(response, list, "物料管理数据");
    }


    /**
     * 查询物料列表
     */
    @PreAuthorize(hasPermi="wms:items:itemName")
    @GetMapping("/itemName")
    public TableDataInfo findItemName(WmsItems wmsItems)
    {
        startPage();
        List<WmsItems> list = wmsItemsService.findItemName(wmsItems);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @PreAuthorize(hasPermi = "wms:wmsItems:add")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WmsItems wmsItems)
    {
        if (UserConstants.NOT_UNIQUE.equals(wmsItemsService.checkItemsNameUnique(wmsItems.getItemName())))
        {
            return AjaxResult.error("新增物料失败，物料名称已存在");
        }else if(UserConstants.NOT_UNIQUE.equals(wmsItemsService.checkItemsNumUnique(wmsItems.getItemNum()))){
            return AjaxResult.error("新增物料失败，物料编号已存在");
        }
        return toAjax(wmsItemsService.insertItems(wmsItems));
    }

    /**
     * 修改物料管理
     */
    @PreAuthorize(hasPermi = "wms:wmsItems:edit")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsItems wmsItems)
    {
        return toAjax(wmsItemsService.updateItems(wmsItems));
    }

    /**
     * 删除物料管理
     */
    @PreAuthorize(hasPermi = "wms:wmsItems:remove")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemUuids}")
    public AjaxResult remove(@PathVariable String[] itemUuids)
    {
        return toAjax(wmsItemsService.deleteItemsByUuids(itemUuids));
    }

    @Log(title = "物料导入", businessType = BusinessType.IMPORT)
    @PreAuthorize(hasPermi = "wms:wmsItems:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WmsItems> util = new ExcelUtil<WmsItems>(WmsItems.class);
        List<WmsItems> itemList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = wmsItemsService.importItem(itemList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<WmsItems> util = new ExcelUtil<WmsItems>(WmsItems.class);
        util.importTemplateExcel(response, "用户数据");
    }

}
